package com.global.dao.impl;

import com.global.Constants;
import com.global.dao.SystemTypeDao;
import com.global.model.SystemType;
import com.global.vo.DataType;
import com.global.vo.WebObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * This class interacts with Spring's HibernateTemplate to save/delete and
 * retrieve User objects.
 *
 * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
 *   Modified by <a href="mailto:dan@getrolling.com">Dan Kibler</a>
 *   Extended to implement Acegi UserDetailsService interface by David Carter david@carter.net
 *   Modified by <a href="mailto:bwnoll@gmail.com">Bryan Noll</a> to work with 
 *   the new BaseDaoHibernate implementation that uses generics.
*/
@Repository("systemTypeDao")
public class SystemTypeDaoImpl extends GenericDaoHibernate<SystemType, Long> implements SystemTypeDao {

    /**
     * Constructor that sets the entity to SystemType.class.
     */
    public SystemTypeDaoImpl() {
        super(SystemType.class);
    }
	
    /**
     * {@inheritDoc}
     */
	public SystemType saveSystemType(SystemType systemType) {
		if(systemType!=null){
	        if (log.isDebugEnabled()) {
	            log.debug("systemType's id: " + systemType.getId());
	        }
	        getHibernateTemplate().saveOrUpdate(systemType);
	        getHibernateTemplate().flush();
		}
		
		return systemType;
	}
	
    /**
     * {@inheritDoc}
     */
	public boolean saveBatchSystemType(List<SystemType> systemTypeList) {
		if(systemTypeList!=null){
			getHibernateTemplate().saveOrUpdateAll(systemTypeList);
			getHibernateTemplate().flush();
			
			return true;
		}
		
		return false;
	}
	
    /**
     * {@inheritDoc}
     */
	public List<SystemType> getSystemTypeListByCategory(String category) {
		if(StringUtils.isNotBlank(category)){
			HashMap<String, Object> queryParams = new HashMap<String, Object>();
	        queryParams.put("category", category);
	        
	        return this.findByNamedQuery("findSystemTypeListByCategory", queryParams);
		}

		return new ArrayList<SystemType>();
	}
	
    /**
     * {@inheritDoc}
     */
	public DataType getDataType(String uniqueName) {
        HashMap<String, Object> queryParams = new HashMap<String, Object>();
        queryParams.put("name", uniqueName);
		List<SystemType> list = this.findByNamedQuery("findSystemTypeByName", queryParams);
		if(!list.isEmpty()){
			return new DataType(list.get(0));
		}
		
		return null;
	}
    /**
     * {@inheritDoc}
     */
	public List<DataType> getDataTypeList() {     
		List<SystemType> sList = this.getSystemTypeListByCategory(Constants.TYPE_DATA_TYPE);
		ArrayList<DataType> tList = new ArrayList<DataType>(sList.size());
		for(SystemType s : sList){
			tList.add(new DataType(s));
		}
		
		return tList;
	}
    
    /**
     * {@inheritDoc}
     */
	public WebObject getWebObject(String name) {
        HashMap<String, Object> queryParams = new HashMap<String, Object>();
        queryParams.put("name", name);
		List<SystemType> list = this.findByNamedQuery("findSystemTypeByName", queryParams);
		if(!list.isEmpty()){
			return new WebObject(list.get(0));
		}
		
		return null;
	}
    /**
     * {@inheritDoc}
     */
	public List<WebObject> getWebObjectList() {
		List<SystemType> sList = this.getSystemTypeListByCategory(Constants.TYPE_WEB_OBJECT);
		ArrayList<WebObject> tList = new ArrayList<WebObject>(sList.size());
		for(SystemType s : sList){
			tList.add(new WebObject(s));
		}
		
		return tList;
	}
}
